#ifndef VIDEO_H
#define VIDEO_H

#include <QCoreApplication>
#include <QObject>
#include <QUdpSocket>
#include <QBuffer>
#include <QImage>
#include <QPixmap>
#include <QLabel>
#include <QDebug>

struct PackageHeader
{
    unsigned int packSize;  //本包头大小

    unsigned int imgID;                 //图片ID
    unsigned int imgSize;             //图片数据的总大小

    unsigned int frameDataLen;          //拆包长度
    unsigned int frameNum;              //数据被分成包的个数
    unsigned int currentFrameNum;
    
    unsigned int dataOffset;            //数据包在整个数据中的偏移
    unsigned int dataSize;              //当前包头的大小(sizeof(PackageHeader)+当前数据包长度)
    unsigned int addCheck;
};

class Video : public QObject
{
    Q_OBJECT
public:
    explicit Video(QObject *parent = nullptr);
    void setShowVedioLab(QLabel *lab)
    {
        showLab = lab;
    }
    quint16 setUdpPort(quint16 port)
    {
        if(port == 0)
        {
            qDebug()<<"cannot set port:0 ...";
            return udpPort;
        }
        if(myUdp->state() == QUdpSocket::ConnectedState)  //有连接状态下不可修改端口号
        {
            qDebug()<<"udp have connected...";
            return udpPort;
        }
        bool res = myUdp->bind(QHostAddress::AnyIPv4,port,QUdpSocket::ReuseAddressHint);
        if(res)
            udpPort = port;
        else
            qDebug()<<"faild to set udp port...";
        return udpPort;
    }

    void imgSend(QImage *myImg, QUdpSocket *udp, QString strIP, quint16 port);
signals:

public slots:
    void onUdpReceive();

private:
    QUdpSocket *myUdp;
    quint16 udpPort;
    QByteArray receivingImg;        //接收中的图片数据
    QImage *buffImg;                 //图片缓存器
    QLabel *showLab;                //实际显示图片的lab
    bool paraImg(QByteArray &datagram, QImage *myImg);   //解析图片
    void Delay(int msec);
};

#endif // VIDEO_H
